const pool = require('../config/db');
const { v4: uuidv4 } = require('uuid');

const Comment = {
    async create({ post_id, user_id, content }) {
        const comment_id = uuidv4();
        await pool.query(
            'INSERT INTO comments (comment_id, post_id, user_id, content) VALUES (?, ?, ?, ?)',
            [comment_id, post_id, user_id, content]
        );
        return { comment_id };
    },

    async findByPostId(post_id, limit = 10, offset = 0) {
        const [rows] = await pool.query(
            'SELECT c.*, u.username, u.full_name FROM comments c JOIN users u ON c.user_id = u.user_id WHERE c.post_id = ? AND c.is_active = TRUE ORDER BY c.created_at DESC LIMIT ? OFFSET ?',
            [post_id, limit, offset]
        );
        return rows;
    }
};

module.exports = Comment;